<template>
	<div class="app-container">
		<view class="list-cell b-b m-t" @click="$utils.jumpTo('/pages/user/detail')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">个人资料</text>
			<text class="cell-more iconfont icon-right"></text>
		</view>
		<view class="list-cell b-b" @click="$utils.jumpTo('收货地址')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">收货地址</text>
			<text class="cell-more iconfont icon-right"></text>
		</view>
		<view class="list-cell" @click="$utils.jumpTo('实名认证')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">实名认证</text>
			<text class="cell-more iconfont icon-right"></text>
		</view>
		<view class="list-cell m-t">
			<text class="cell-tit">消息推送</text>
			<switch checked color="#39b54a" @change="switchChange" />
		</view>
		<view class="list-cell m-t b-b" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">清除缓存</text>
			<text class="cell-tip" @click="cleanStorage">{{ storageSize }}</text>
			<text class="cell-more iconfont icon-right" @click="cleanStorage"></text>
		</view>
		<view class="list-cell b-b" @click="$utils.jumpTo('关于浪亭')" hover-class="cell-hover" :hover-stay-time="50">
			<text class="cell-tit">关于浪亭</text>
			<text class="cell-more iconfont icon-right"></text>
		</view>
		<view class="list-cell">
			<text class="cell-tit">检查更新</text>
			<text class="cell-tip">当前版本 1.0.0</text>
			<text class="cell-more iconfont icon-right"></text>
		</view>
		<view class="list-cell log-out-btn" @click="toLogout">
			<text class="cell-tit">退出登录</text>
		</view>
	</div>
</template>

<script>
	import { mapMutations } from 'vuex'

	export default {
		data() {
			return {
				storageSize: '正在计算'
			}
		},
		onShow() {
			this.initData()
		},
		methods: {
			...mapMutations(['logout']),
			// 初始化
			initData() {
				// #ifdef APP-PLUS
				this.getStorage()
				// #endif
			},
			// 退出登录
			toLogout() {
				uni.showModal({
					content: '确定要退出登录么',
					success: (e) => {
						if (e.confirm) {
							this.logout()
							setTimeout(() => {
								uni.navigateBack()
							}, 200)
						}
					}
				})
			},
			// 消息推送
			switchChange(e) {
				let statusTip = e.detail.value ? '打开' : '关闭'
				this.$utils.msg(`${statusTip}消息推送`)
			},
			// 清除缓存
			cleanStorage() {
				uni.showModal({
					title:'提示',
					content:'确定清除缓存吗?',
					success: res => {
						if(res.confirm){
							plus.cache.clear(() => {
								this.getStorage()
								this.$utils.msg('清除成功', 1500, false, 'success')
							});
						}
					}
				})
			},
			// 获取缓存
			getStorage(){
				plus.cache.calculate(( size ) => {
					if(size < 1024){
						this.storageSize = size + 'B'
					}
					else if(size / 1024 >= 1 && size / 1024 / 1024 < 1){
						this.storageSize = Math.floor(size / 1024 * 100) / 100 + 'KB'
					}
					else if(size / 1024 / 1024 >= 1){
						this.storageSize = Math.floor(size / 1024 / 1024 * 100) / 100 + 'M'
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.list-cell{
		display: flex;
		align-items: baseline;
		padding: 20rpx 30rpx;
		line-height: 60rpx;
		position: relative;
		background: $bg-color;
		justify-content: center;
		&.log-out-btn{
			margin-top: 40rpx;
			.cell-tit{
				color: $primary;
				text-align: center;
				margin-right: 0;
			}
		}
		&.cell-hover{
			background:#fafafa;
		}
		&.b-b:after{
			left: 30rpx;
		}
		&.m-t{
			margin-top: 16rpx; 
		}
		.cell-more{
			align-self: baseline;
			font-size: 32rpx;
			color: #999;
			margin-left:10rpx;
		}
		.cell-tit{
			flex: 1;
			font-size: 30rpx;
			color: #333;
			margin-right:10rpx;
		}
		.cell-tip{
			font-size: 28rpx;
			color: #333;
		}
		switch{
			transform: translateX(16rpx) scale(.84);
		}
	}
</style>
